import { toTree, getReqUrl,checkNetWork } from '../assets/js/utils'
import Vue from 'vue'
/*引入utils */
import instance from '@/assets/js/utils'

//获取部门
export const getDept = ({ commit, state }, obj) => {
  if (checkNetWork()) {
    var _this = obj.vm
    return new Promise((resolve, reject) => {
      instance({
        method: 'post',
        url:_this.COMMON_URL,
          // _this.COMMON_URL +
          // '?_method=V_USER_SELECT*getHRGroupTreeList&type=prj&projectId=0',
        data: {
          _method : "V_USER_SELECT*getHRGroupTreeList",
          type : "prj",
          projectId : '0',
          pageIndex: 0,
          pageSize: 10
        }
      })
        .then(res => {
          console.log("tree",res);
          if (obj.id == 0) {
            var deptTree = obj.vm.toTree(res.data.treedata)
            console.log("部门树123",deptTree)
            commit('setDept', deptTree)
          } else {
            obj.callback(res.data)
          }
        })
        .catch(err => {
          console.log(err)
        })
    })
  } else {
    //执行离线状态时的任务   
    state.SQLiteDB.transaction((tx) => {
      tx.executeSql('SELECT ID,NAME,ORG_PATHID,ORG_PATHNAME,PARENTID FROM DEPT_TREE',[],(tx,result)=>{
        console.log("结果",result);
        
        var temp = [];
        for(var i=0;i<result.rows.length;i++){
          temp[i] = result.rows.item(i);
        }
        var deptTree = obj.vm.toTree(temp);
        commit('setDept', deptTree)
      });
    }, (err) => {
        console.log('错误', err)
    })
  }
};
export const getBadge = ({ commit },obj)=>{
  var _this = obj.vm;
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url: _this.COMMON_URL,
      data: {
        _method : "formflow.getTaskListCount",
        filter : "( flag in ('0','1','201','401'))"
      },
    })
      .then(res => {
        // console.log("shenmedongxi",res);
        commit('setTaskListCount', res.data.data)
      })
      .catch(err => {
        console.log(err)
      })
  })
};
export const getSystemCount = ({ commit },obj)=>{
  var _this = obj.vm;
  var userId = obj.item.USER_NO
  var total = 0;
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url: _this.COMMON_URL,
      data: {
        _method : "SYS_NOTICE.getNotice",
        loginUserId : userId
      },
    }).then(res => {
      // console.log("denglu1111",res);
      total = total + res.data.noticeList.length
      commit("setSystemCount",total);
    }).catch(err => {
       console.log(err)
    })
    // instance({
    //   method: 'post',
    //   url: _this.COMMON_URL,
    //   data: {
    //     _method : "EMERGENCYNOTICE.getNews",
    //   },
    // })
    //   .then(res => {
    //     // console.log("shenmedongxi",res);
    //     // console.log("denglu",res);
    //     total = total + res.data.data.length;
    //   })
    //   .catch(err => {
    //     console.log(err)
    //   }).then(()=>{
    //     instance({
    //       method: 'post',
    //     url: _this.COMMON_URL,
    //     data: {
    //       _method : "SYS_NOTICE.getNotice",
    //       loginUserId : userId
    //     },
    //     }).then(res => {
    //       // console.log("denglu1111",res);
    //       total = total + res.data.noticeList.length
    //       commit("setSystemCount",total);
    //     })
    //     .catch(err => {
    //       console.log(err)
    //     })
    //   })
  })

}
